<?php
namespace app\admin\controller\soft;
use app\providers\BaseController;
use app\admin\model\SystemParam;
use app\admin\model\SystemParamValue;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("系统参数")
 * @Apidoc\Group("base")
 * @Apidoc\Sort(1)
 */
class Param extends BaseController {
	
	
	/**
    * 获取系统参数
    *  @Apidoc\Author("weifashi")
    * @Apidoc\Url("admin/soft/param/getParam")
    * @Apidoc\Method("POST")
    */
	public function getParam(){
		$result = ( new SystemParam )-> getParam();
		return $result[0] ? $result : [];
	}
	
	
	/**
    * 获取文本参数
    *  @Apidoc\Author("weifashi")
    * @Apidoc\Url("admin/soft/param/getTextParamValue")
    * @Apidoc\Method("POST")
    * @Apidoc\Param("param_scope", require=true,default="",desc="参数范围" )
    * @Apidoc\Param("param_code", require=true, default="",desc="参数编码" )
    */
	public function getTextParamValue($param_scope,$param_code,$isEcho=true){
		if(!$param_scope || !$param_code){
			return \Api::ReError("param_scope或param_code参数没传！");
		}	
		$result = ( new SystemParamValue )->getTextParamValue($param_scope,$param_code);
		return !$isEcho ? $result : \Api::ReSucess($result);
	}
    
    /**
    * xml获取json参数
    *  /admin/soft/param/xmlGetJsonParam
    * @access public
    * @param string $param_scope            参数范围
    * @param string $param_code             参数编码
    * @param array $isReturn                    直接输出，还是返回数据
    * @return string
    */
    public function xmlGetJsonParam($param_scope='',$param_code=''){
        $result = ( new SystemParamValue )->getTextParamValue($param_scope,$param_code);
        if($result){
            return  json_decode($result,true);
        }
        return [];
    }
	
	
	/**
    * 保存参数值
    *  @Apidoc\Author("weifashi")
    * @Apidoc\Url("admin/soft/param/saveTextParamValue")
    * @Apidoc\Method("POST")
    * @Apidoc\Param("param_scope", require=true,default="",desc="参数范围" )
    * @Apidoc\Param("param_code", require=true, default="",desc="参数编码" )
    * @Apidoc\Param("param_value", require=true, default="",desc="value值" )
    */
	public function saveTextParamValue( $param_scope ,$param_code,$param_value){
		if(!$param_scope || !$param_code ){
			return \Api::ReError("param_scope或param_code参数没传！");
		}	
		$result = ( new SystemParamValue )->saveTextParamValue( request()->post() );
		if( $result !=1  ){
			return \Api::ReError("请求错误：".$result);	
		}	
		return \Api::ReSucess($result);	
	}
	
   	/**
    * 保存参数值-多个
    *  @Apidoc\Author("weifashi")
    * @Apidoc\Url("admin/soft/param/saveParams")
    * @Apidoc\Method("POST")
    * @Apidoc\Param("param_scope", require=true,default="",desc="参数范围" )
    * @Apidoc\Param("param_code", require=true, default="",desc="参数编码" )
    * @Apidoc\Param("param_value", require=true, default="",desc="value值" )
    * @Apidoc\Param("param_text", require=true, default="",desc="text值" )
    */
	public function saveParams($param_scope,$param_code,$param_value,$param_text){
		if(!$param_scope || !$param_code || !$param_value || !$param_text){
			return \Api::ReError("param_scope或param_code或param_value参数没传！");
		}	
		$result = ( new SystemParamValue )->saveParam( request()->post() );
		if( $result !=1  ){
			return \Api::ReError("请求错误：".$result);	
		}	
		return \Api::ReSucess($result);	
	}
	
   /**
    * 获取参数
    *  @Apidoc\Author("weifashi")
    * @Apidoc\Url("admin/soft/param/getParamValue")
    * @Apidoc\Method("POST")
    * @Apidoc\Param("param_scope", require=true,default="",desc="参数范围" )
    * @Apidoc\Param("param_code", require=true, default="",desc="参数编码" )
    */
	public function getParamValue($param_scope,$param_code,$vid=false,$is_action_call=false){
		if(!$param_scope || !$param_code ){
			return \Api::ReError("param_scope或param_code没传！");
		}	
		$result = ( new SystemParamValue )->getParams($param_scope,$param_code);
		if( $result !=1  ){
			return \Api::ReError("请求错误：".$result);	
		}
		foreach($result as $val){
			$val['value'] = $vid ? $val['id'] : $val['param_value'];
			$val['label'] = $val['param_text'];
		}
		if($is_action_call){
			return $result;	
		}else{
			return \Api::ReSucess($result);	
		}
	}
	
	
	/**
     * 排序调整
     * @Apidoc\Desc("排序调整")
     * @Apidoc\Author("weifashi")
     * @Apidoc\Url("admin/soft/param/sortMove")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("moveId",type="int",require=true,default="1",desc="id1" )
     * @Apidoc\Param("coverId", type="int",require=true, default="2",desc="id2" )
     */
	public function sortMove($moveId,$coverId){
		if( empty($moveId) )
			throw new \think\Exception('moveId参数不能为空');
		if( empty($coverId) )
			throw new \think\Exception('coverId参数不能为空');
		
		if($moveId==-1 || $coverId==-1){
			return \Api::ReSucess("","操作成功");
		} 
		
		$result = ( new SystemParamValue )->updateSort($moveId,$coverId);
		if($result){
			return \Api::ReSucess("","操作成功");
		}else{
			return \Api::ReError("未知错误！");
		}
		
	}
	
}



